<?php
/**
* Function: 
*	This page builds the control for the join.php page.
**/
function joiner_edit( $user_ident ) {
    global $metatags;
    global $JOINER_CFG;
  
    
    // Add the javascript validation.
    $url = url;
    
    // Error messages
    $required = __gettext('You must fill out every field.');
    $maxlen= __gettext('This field can not be this long, please limit it to 45 characters or less.');

    
//  BUILD JAVASCRIPT
   
    // Build javascript for field validation (requiring elements)
	$metatags .= <<< END
		<SCRIPT type="text/javascript" src="{$url}mod/joiner/yav1_2_3/js_compact/yav.js"></SCRIPT>
		<SCRIPT type="text/javascript" src="{$url}mod/joiner/yav1_2_3/js_compact/yav-config.js"></SCRIPT>
        <SCRIPT>
            var rules=new Array();
            
END;

    $i = 0;
    // Build individual rules
    foreach ( $JOINER_CFG->fields as $field ) {
        // Load each field name into metatags.

        if ( $field['type'] == 'select' ) {
            // selection type
            $metatags .= "    rules[" . $i++ . "]='joiner_{$field['name']}|required|{$required}';\n";
        } elseif ( $field['type'] == 'input' ) {
            // input box type
            $metatags .= "    rules[" . $i++ . "]='joiner_{$field['name']}|required|{$required}';\n";            
            $metatags .= "    rules[" . $i++ . "]='joiner_{$field['name']}|maxlength|45|{$maxlen}';\n";            
        } 

    }
    
    // Close off script
        $metatags .= "  </SCRIPT>\n";

        
//  BUILD BODY
    
    // Begin building body
    $body .= <<< END
        <form method="post" name="elggform" 
            action="{$url}mod/joiner/html/action_redirection.php" 
            onsubmit="return performCheck('elggform', rules, 'classic');">
        <ul>
END;

    // Load each fields.
    foreach ( $JOINER_CFG->fields as $field ) {
        // Load name into form
        $body .= '<li>' . $field['name'] . "<br/>\n";
        
        // Branch on type of the field.
        if ( $field['type'] == 'select' ) {
            // selection
            $body .= '<select name="joiner_' . $field['name'] . '">';
            // Build each option
            foreach ( $field['values'] as $value ) {
                $body .= '<option value="' . $value. '">' . $value . '</option>';
            }
            $body .= '</select>';
            
        } elseif ( $field['type'] == 'input' ) {
            // input box
            $body .= '<input length=45 maxlength=45 name="joiner_' . $field['name'] . '"/>';
            
        } else {
            // error
            die('Unknown type value passed to join.php from /mod/joiner/config/php');
        }
    }

    // Finish building form.
    $body .= <<< END
        </ul> 
   		<input type="hidden" name="action" value="joiner:update" />
		<input type="submit" value="Save" />
        </form>
END;
//		<input type="hidden" name="joiner_user_ident" value="' . $user_ident . '" />
    return $body;
}
	
?>